package com.dcjt.firehotcarpad.helper;

import android.text.TextUtils;

import com.amap.api.location.CoordinateConverter;
import com.amap.api.location.DPoint;
import com.amap.api.maps.model.LatLng;
import com.dcjt.firehotcarpad.base.SPConstant;
import com.dcjt.firehotcarpad.entity.UserEntity;
import com.dcjt.firehotcarpad.manager.DataManager;
import com.dcjt.firehotcarpad.utils.Converter;

/**
 * Created by cj on 2021/3/8.
 * Email:codesexy@163.com
 * Function:
 * desc:
 */
public class DetermineHelper {


    private int MAX_COUNT = 5;

    private volatile int count_off_camp = 0;

    private volatile int count_arrive_scene = 0;//

    private volatile int count_back_camp = 0;

    private DetermineHelper() {
    }

    private static DetermineHelper instance;

    public static DetermineHelper getInstance() {
        if (instance == null) {
            synchronized (DetermineHelper.class) {
                instance = new DetermineHelper();
            }
        }
        return instance;
    }


    public boolean isOffCamp(DPoint point) {
        UserEntity userEntity = DataManager.getInstance().getUser();
        if (point == null || userEntity == null || TextUtils.isEmpty(userEntity.getX()))
            return false;

        try {
            // 营区位置   转为高德地图坐标
            DPoint campPoint =  Converter.INSTANCE.toGCJ02Point_DPoint(Double.parseDouble(userEntity.getY()), Double.parseDouble(userEntity.getX()));
            float distance = CoordinateConverter.calculateLineDistance(point, campPoint);
            if (distance > Integer.parseInt(SPConstant.CAR_DISTANCE)) {
                count_off_camp++;
                if (count_off_camp < MAX_COUNT) {
                    return false;
                } else {
                    //连续5次超过阈值 判断条件满足
                    count_off_camp = 0;
                    return true;
                }
            } else {
                //只要有一次是小于阈值就重新计数
                count_off_camp = 0;
                return false;
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            count_off_camp = 0;
            return false;
        }
    }


    public boolean isArriveScene(DPoint scenePoint, DPoint carPoint) {
        if (scenePoint == null || carPoint == null) {
            return false;
        }

        try {
            //灾情点位置转坐标
            DPoint carPoint_zm = Converter.INSTANCE.toGCJ02Point_DPoint(carPoint.getLatitude(),carPoint.getLongitude());
//            DPoint carPoint_zm = Converter.INSTANCE.toGCJ02Point_DPoint(30.574762622548917,104.0604881049515);
            float distance = CoordinateConverter.calculateLineDistance(scenePoint, carPoint_zm);
            if (distance < Integer.parseInt(SPConstant.CAR_DISTANCE)) {
                count_arrive_scene++;
                if (count_arrive_scene < MAX_COUNT) {
                    return false;
                } else {
                    //连续5次超过阈值 判断条件满足
                    count_arrive_scene = 0;
                    return true;
                }
            } else {
                //只要有一次是小于阈值就重新计数
                count_arrive_scene = 0;
                return false;
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            count_arrive_scene = 0;
            return false;
        }
    }


    public boolean isBackCamp(DPoint carPoint) {
        UserEntity userEntity = DataManager.getInstance().getUser();

        if (userEntity == null || userEntity == null || TextUtils.isEmpty(userEntity.getX()))
            return false;

        try {
            // 营区位置   转为高德地图坐标
            DPoint campPoint =  Converter.INSTANCE.toGCJ02Point_DPoint(Double.parseDouble(userEntity.getY()), Double.parseDouble(userEntity.getX()));
            float distance = CoordinateConverter.calculateLineDistance(campPoint, carPoint);
            if (distance < Integer.parseInt(SPConstant.CAR_DISTANCE)) {

                count_back_camp++;
                if (count_back_camp < MAX_COUNT) {
                    return false;
                } else {
                    //连续5次超过阈值 判断条件满足
                    count_back_camp = 0;
                    return true;
                }
            } else {
                //只要有一次是小于阈值就重新计数
                count_back_camp = 0;
                return false;
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            count_back_camp = 0;
            return false;
        }
    }

}
